package application;

public class Calculator {

    String expr;
    intStack s;

    public Calculator(String expression) {

        s = new intStack();
        this.expr = (expression.length() == 0 ? "0" : expression);
    }

    public void calculate() {
        for (int i = 0; i < expr.length(); i++) {
            if (expr.charAt(i) != '+' && expr.charAt(i) != '-' && expr.charAt(i) != '*' && expr.charAt(i) != '/') {
                Character ch = new Character(expr.charAt(i));
                s.push(Integer.parseInt(ch.toString()));
            } else {
                if (expr.charAt(i) == '+') {
                    int i1 = s.pop();
                    int i2 = s.pop();
                    s.push(i2 + i1);
                }
                if (expr.charAt(i) == '-') {
                    int i1 = s.pop();
                    int i2 = s.pop();
                    s.push(i2 - i1);
                }
                if (expr.charAt(i) == '*') {
                    int i1 = s.pop();
                    int i2 = s.pop();
                    s.push(i2 * i1);
                }
                if (expr.charAt(i) == '/') {
                    int i1 = s.pop();
                    int i2 = s.pop();
                    s.push(i2 / i1);
                }
            }
        }
        System.out.println(s.size() > 1 ? "Error in expression" : "result: " + s.getlast());
    }
}
